package cn.flyboy.utils;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import cn.flyboy.entity.StaticResources;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLHelper {
    public static String path;  // 文件路径

    public static Map xmlToMap(String xmlFilePath) {
        Map res = null;
        // 解析xml文件
        // 创建SAXReader的对象reader
        SAXReader reader = new SAXReader();
        reader.setEncoding("utf8");//这里设置文件编码
        //创建文档流
        FileInputStream in;
        try {
            // 通过reader对象的read方法加载xml文件,获取docuemnt对象。
            in = new FileInputStream(xmlFilePath);
            Document document = reader.read(in);
            // 通过document对象获取根节点
            Element root = document.getRootElement();

            //将root转化为map
            res = getMap(root);

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 递归的将一个document转化为map
     *
     * @param root
     * @return
     */
    private static Map getMap(Element root) {
        // 通过element对象的elementIterator方法获取迭代器
        Iterator<Element> it = root.elementIterator();
        Map res = new HashMap<>();//保存父元素

        // 遍历迭代器，获取根节点中的信息
        while (it.hasNext()) {
            Element ele = it.next();
            String key = ele.getName();
            System.out.print(key + " ");
            System.out.println(ele.getStringValue());
            res.put(key, ele.getStringValue());
        }

        return res;
    }


    public static boolean updateXml(String xmlFilePath, Map<String, String> config) throws IOException {
        File f = new File(xmlFilePath);
        if (config == null || !f.exists()) return false;
        //遍历需要修改的值
        Document doc = getDocument(f.getPath());
        Element root = doc.getRootElement();

        Iterator<Element> itor = root.elementIterator();
        while (itor.hasNext()) {
            root = itor.next();
            String text = "";
            if ((text = config.get(root.getName())) != null) {
                root.setText(text);
                System.out.println("更新" + root.getName() + "的值为：" + text);
            }
        }

        XMLWriter output = new XMLWriter(new FileWriter(f));
        output.write(doc);
        output.close();
        System.out.println("更新配置完成");
        return true;
    }


    /**
     * 得到XML文档
     *
     * @param xmlFilePath（路径）
     * @return XML文档对象
     * @throws DocumentException
     */
    public static Document getDocument(String xmlFilePath) {
        SAXReader reader = new SAXReader();
        reader.setEncoding("utf8");//这里设置文件编码
        File file = new File(xmlFilePath);
        try {
            if (!file.exists()) {
                return null;
            } else {
                return reader.read(file);
            }
        } catch (DocumentException e) {
            throw new RuntimeException(e + "->指定文件【" + xmlFilePath + "】读取错误");
        }
    }


    public static void main(String[] args) {
        path = System.getProperty("user.dir") + "/src/main/webapp/assets/config.xml";

        Map<String, String> config = new HashMap<>();
        config.put(StaticResources.SPLIT_PAGE_COUNT, "" + 10);
        try {
            updateXml(path, config);
            xmlToMap(path);
        } catch (IOException e) {
            System.out.println("更新配置文件失败");
            e.printStackTrace();
        }
    }
}